# coding: utf-8
# @Author: cyl
# @File: 06txt.py
# @Time: 2024/08/19 21:47:52


# 原格式读取: 常用, 但大文件性能一般
print("---- read ----")
with open("/Users/mac/Downloads/python_study/2库/test.txt", mode="r", encoding="utf-8") as f:
    print(f.read())

# 读一行: 比较推荐
print("---- readline ----")
with open("/Users/mac/Downloads/python_study/2库/test.txt", mode="r", encoding="utf-8") as f:
    print(f.readline())

# 将读取的数据转为数组: 数据量比较小
print("---- readlines ----")
with open("/Users/mac/Downloads/python_study/2库/test.txt", mode="r", encoding="utf-8") as f:
    print(f.readlines())
    
# 写
print("---- write ----")
with open("/Users/mac/Downloads/python_study/2库/test.txt", mode="r", encoding="utf-8") as f:
    f.write("123456")

# 之前的写法
file = open("/Users/mac/Downloads/python_study/2库/test.txt", mode="r", encoding="utf-8")
content = file.read()
file.close()

# python为了简化这种写法, 出了一个with上下文 -> 1、减少了文件io的打开和关闭的操作 2、自动关闭文件
